import org.apache.spark.sql.{DataFrame, SparkSession}

object Demo03SparkSQL02 {
  def main(args: Array[String]): Unit = {
    val spark: SparkSession = SparkSession
      .builder()
      .appName(this.getClass.getSimpleName.replace("$", ""))
      .master("local")
      .config("spark.sql.shuffle.partitions", "2")
      .enableHiveSupport() // 开启Hive的支持
      .getOrCreate()

    // 导入隐式转换以及函数包
    import spark.implicits._
    import org.apache.spark.sql.functions._

    // 加载Hive中的数据
    val stuDF: DataFrame = spark.table("test.students_hive")

    stuDF.printSchema()

    stuDF
      .groupBy($"gender")
      .agg(countDistinct("id") as "cnt")
      .show()
  }

}
